package com.hyfrogx.modules.manage.controller.course;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.exception.ServiceException;
import com.hyfrogx.common.param.JSONResult;
import com.hyfrogx.common.qcloud.UploadUtils;
import com.hyfrogx.common.utils.Constant;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.app.utils.StringUtils;
import com.hyfrogx.modules.manage.controller.BaseController;
import com.hyfrogx.modules.manage.entity.course.CourseManageEntity;
import com.hyfrogx.modules.manage.entity.course.CourseTypeEntity;
import com.hyfrogx.modules.manage.form.course.UpdateCourseTypeForm;
import com.hyfrogx.modules.manage.service.course.CourseManageService;
import com.hyfrogx.modules.manage.service.course.CourseTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @author 罗春杰-技术部
 */
@RestController
@RequestMapping("/course")
@Api(value = "CourseTypeController",tags = {"后台-课程分类"})
public class CourseTypeController extends BaseController {

    @Autowired
    private CourseTypeService typeService;

    @Autowired
    private CourseManageService manageService;

    /**
     * 上传COS
     * @param file
     * @return
     */
    @ApiOperation("COS上传")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        // 文件路径
        String url = "";
        // 腾讯云
        url = UploadUtils.uploadWebFile(file, "course");
        return R.ok().put("filename",url);
    }

    @PostMapping("getAllCourseTypeByStatus")
    @ApiOperation("获取所有的分类")
    public R getAllCourseType(){
        List<CourseTypeEntity> list = typeService.list(new QueryWrapper<CourseTypeEntity>()
                .eq("deleted", 1)
                .orderBy(true, false, "sort"));
        return R.ok().put("data",list);
    }

    @PostMapping("sort")
    @ApiOperation("更新类别排序")
    public R sort(String info){
        if (StringUtils.isEmpty(info)) {
            return R.error();
        }
        JSONArray ja = JSONArray.parseArray(info);
        List<CourseTypeEntity> list = new ArrayList<CourseTypeEntity>();
        for (int i = 0; i < ja.size(); i++) {
            CourseTypeEntity object = ja.getObject(i, CourseTypeEntity.class);
            list.add(object);
        }
        try {
            typeService.updateSort(list);
            return R.ok();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }
    @PostMapping("deleteCourseType")
    @ApiOperation("逻辑删除")
    public R deleteCourseType(@RequestParam Integer id){
        try {
            int count = manageService.count(new QueryWrapper<CourseManageEntity>().eq("classify", id));
            if(count>0){
                return R.error("分类下有课程");
            }
            typeService.removeType(id);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok("删除成功");
    }

    @GetMapping("getAllCourseType")
    @ApiOperation("获取分页课程分类")
    public R getAllCourseType(@RequestParam Map<Object,Object> map){
        try {
            Page page = typeService.selectTypePage(new QueryExt(map));
            return R.ok().put("data",page);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }

    @PostMapping("updateCourseType")
    @ApiOperation("编辑课程分类")
    public R updateCourseType(@RequestBody UpdateCourseTypeForm form,@RequestParam Integer id){
        try {
            CourseTypeEntity typeEntity = typeService.getById(id);
            if(typeEntity.getStatus()==1){
                return R.error("分类处于上线中");
            }
            BeanUtils.copyProperties(form,typeEntity);
            typeService.updateById(typeEntity);
            return R.ok("编辑成功");
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }

    @PostMapping("addCourseType")
    @ApiOperation("新增课程分类")
    public R addCourseType(@Validated @RequestBody UpdateCourseTypeForm form){
        try {
            CourseTypeEntity courseTypeEntity = new CourseTypeEntity();
            BeanUtils.copyProperties(form,courseTypeEntity);
            courseTypeEntity.setCreateTime(new Date());
            typeService.addCourseType(courseTypeEntity);
            return R.ok("新增成功");
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }

    @PostMapping("updateCourseStatus")
    @ApiOperation("分类上线下线")
    public R addCourseType(@RequestParam Integer id){
        try {
            CourseTypeEntity typeEntity = typeService.getById(id);
            if(typeEntity.getStatus().equals(FrogConstant.CourseStatus.UPPER)){
                typeEntity.setStatus(FrogConstant.CourseStatus.LOWER);
            }else{
                typeEntity.setStatus(FrogConstant.CourseStatus.UPPER);
            }
            typeService.updateById(typeEntity);
            return R.ok("成功");
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }


}
